package com.thread.sync;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Synchronized2 {
	private static void common(){
		System.out.println(Thread.currentThread().getName() + "..start.." + "-----" + System.currentTimeMillis());
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println(Thread.currentThread().getName() + "..end.." + "-----" + System.currentTimeMillis());
	
	}
	// 直接在静态方法上加synchronized 线程安全
	public synchronized static void syncStatic() {
		common();
	}

	// 在静态方法上synchronized当前类 线程安全
	public static void syncCurrentClass() {
		synchronized (Synchronized2.class) {
			common();
		}
	}

	// 在成员方法上synchronized当前类 线程安全
	public void syncCurrentObjectByThisClass() {
		synchronized (Synchronized2.class) {
			common();
		}
	}

	public static void main(String[] args) {
		ExecutorService exec = Executors.newFixedThreadPool(3);
		for (int i = 0; i < 5; i++) {
			exec.execute(new Runnable() {
				@Override
				public void run() {
					Synchronized2.syncCurrentClass();
				}
			});
		}
		exec.shutdown();
	}

}
